cs1ch6b.htm
 
CHAPTER 6

Giving Olympia A Better Interface
or
User Interface Design - Some Preliminary Techniques and Implications


  


Table of Contents

1. The Story
of C++!

(The basics for any beginner!)


2. The

of C++

A reference document on the basic elements of C++.



3. The Patterns





Once again we would use the our "While Loop With Priming Read" pattern. The code code (for the 'DisplayInfo' option) would look as follows:

    switch (choice)
    {   case 'D':
    	case 'd':
    	contractNum =
     
        GetContractNum();
    	while (contractNum
    	! = 
    	  0) {
    	switch	(contractNum) {
    		case	1: DisplayInfo(contract1);
    				break;
    				case
    
    			2: DisplayInfo(contract2);
    				break;
    				case
    
    			3: DisplayInfo(contract3);
    				break;
    				//
    
    				The two other cases go here default:
    
    				cout
    				<<  "Invalid contract. Please try again\n";
    		}
    		contractNum = GetContractNum();
    	}
    	break;   // the break statement for case 'd'.
    	
    // The rest of other cases goes here
    } 
    
Notice the complexity of this code. We have an outer while loop (not shown) inside of which we have a 'switch' statement. Inside that we have 'while' loops and embedded switch statements for both the 'display' and 'change' user options. (The exact same while-switch code would be used to handle the user's wish to change a property. Click here to see the full code for this function.)

VI. The Function 'DisplayInfo'

A. Class Instances as Parameters
Our analysis indicates that 'DisplayInfo' receives a contract and returns nothing. Therefore the declaration looks as follows:

void DisplayInfo(Contract contract);

Note how we use 'Contract' (the name of the class) where a type name goes. Since we are declaring a parameter here and the system knows that this local instance will be immediately instantiated with the property values of some contract on the calling side, this usage is OK. What actually happens is that the properties of the actual parameter are copied to the corresponding data members in the local instance.Later we will see situations in which it is necessary to explicitly declare and define what is called a copy constructor

It should be emphasized that it would not be valid to, for example, declare a local variable here (or anywhere else) as: "Contract c1;". Upon compiling you would get an error message similar to:

"Could not find a match for Contract::Contract()....."

In other words, there is no constructor for the class 'Contract' that has no parameters. This, of course, is true since we only declared one constructor for the Contract class and it has three parameters.

B. The Code for DisplayInfo
The definition is relatively straight forward. We simply need to request each piece of information from the instance by calling the appropriate member function and then display it. Recall from the last chapter that we call a member function by typing the instance name, followed by a dot (.), followed by the member function name. Here then is the code:

    void DisplayInfo(Contract contract)
    {
      int sqFootage;
      sqFootage = contract.ProvideSquareFootage();
      cout << "\n\nThe square footage for the contract is: " << sqFootage << endl;

      int numDesks;
      numDesks = contract.ProvideNumberOfDesks();
      cout << "The number of desks for the contract is: " << numDesks << endl;

      int numDays;
      numDays = contract.ProvideNumberOfDays();
      cout << "The number of days for the contract is: " << numDays << endl;

      double charge;
      charge = contract.ProvidePerWeekCharge();
      cout << "The per week charge for the contract is: " << charge << endl;
      cout << endl << endl << endl;

    }

We have added three 'endl' outputs at the end to cause three lines to be skipped on the monitor between this output and whatever happens next. This simply makes the output more readable.

There is a slightly shorter way to write this code. Functions that return values can be placed anywhere a variable can be placed as long as one is careful to match types. For example then, one could drop the variable 'sqFootage' and replace it directly with the function call as in:


   cout << "\n\nThe square footage for the contract is: "
        << contract.ProvideSquareFootage()
        << endl;

Using this approach the value returned by 'ProvideSquareFootage' is sent directly to the output stream instead of being temporarily stored in a local variable whose value is then sent to the stream. You should use whichever approach feels best to you. (Note that in this version the function call does not end with a semi-colon because it is part of a larger instruction.)

VII. The Function 'ChangeInfo'

A. Returning a More Complex Data Type
The declaration for 'ChangeInfo' is different from that of 'DisplayInfo' in that 'ChangeInfo' returns a contract. Therefore we could write the declaration as follows:

Contract ChangeInfo(Contract contract);

If we did this we would have to rewrite the calls to "ChangeInfo' in 'ProcessUserChoices' to, for example:

contract1 = ChangeInfo(contract1);

While this would work, it is not as commonly done as a second way. In chapter four we learned how to use the '&' symbol to allow more than one value to be returned. We noted further that the '&' symbol creates a "two way transmission path" allowing information to not only be returned but also sent via the same connection. In the case of 'ChangeInfo' we already need a path to transmit information from the calling function (in other words, we need to receive a contract) so why not simply use the same path to return the changed contract. The '&' does this. Here is the new declaration:

void ChangeInfo(Contract& contract);

To emphasize the key point: The '&' symbol creates a two way path so that the original information about the contract in question can be received by 'ChangeInfo' and the changed information can be returned. When a call to 'ChangeInfo' such as:

ChangeInfo(contract1);

in 'ProcessUserChoices' is completed the information originally in contract1 has been sent to 'ChangeInfo' and the same information, as modified by 'ChangeInfo' has been sent back. The instance 'contract1' in 'ProcessUserChoices' has now been modified.

B. The Code
We now move on to the definition for 'ChangeInfo'. This time, instead of calling 'Provide.... member functions as we did in 'DisplayInfo', we call 'Change....' member functions to cause the properties to be changed. Before doing that we must, however, get the new value(s) for the property(ies) to be changed and before that we must ask the user which property(ies) she or he wants changed. To make the user interface more efficient, we allow the user to change as many properties as she or he wants - using a sentinel value of 'N' to indicate completion. Here is the code:

    void 	ChangeInfo(Contract& contract)
    {
    	char property;
    
    	property = GetProperty();
    	while ((property != 'N') && (property !=
     	 
    'n'))
    	{ switch
    
    	(property)  {
    	case 'S': case 's': int sqFootage; sqFootage= 
    		GetSquareFootage(); contract.ChangeSquareFootage(sqFootage);
    				break;			case 'K':
    			case 'k':
    				int numDesks;
    				numDesks = GetNumDesks();
    				contract.ChangeNumDesks(numDesks);
    				break;
    			case 'Y':
    			case 'y':
    				int numDays;
    				numDays = 
    				
    				
    			 
    			 
    				 
    				GetNumDays();  contract.ChangeNumDays(numDays);
    				break;
    				default:
    				cout
    				<<  "\nInvalid choice of properties. Please try again\n";
    
    		}  // end switch
    		property = GetProperty();
    	}
    }	
    
Again we have a while statement using the basic pattern we have seen over and over again inside of which there is a switch statement to handle changing the various contract class properties. In each case, except the default, we ask the user for the new value of the property chosen to be changed and then call the appropriate member function to accomplish the change.

One other point: The while test condition is a bit different in that it uses the 'AND' symbol ('&&'). Students are often confused about when to use AND and OR. The confusion comes partially from the way a situation like this is commonly described in English. The specifications are likely to say something like:

"Stop when the user enters 'N' or 'n".

While the specifications statement has an 'or' in it, it gets translated as 'AND' or '&&'. The reason is that the while statement is not concerned with when to stop as when to continue. Remember, the while statement test condition can be translated as:

"while the following is true execute the following.....".

In other words, when translating a phrase such as Stop when the user enters 'N' or 'n" into a C++ while statement, it might be wise to first translate the English into, "while the user has not entered 'N' and has not entered 'n'....". Note how this English captures the meaning of the C++ 'while' statement and, therefore, the translation process (from algorithm to code) is relatively straight forward. The moral of the story is be very careful how you translate English 'and' and 'or' into '&&' and '||'.

VIII. Putting It All Together
The design and coding of the rest of this problem is relatively straight forward. If you are unsure of what remains to be done, make a copy of the structure chart and put a mark next to each module we have discussed. In your own programs you are encouraged to use a similar process. As you design a module put a 'D' next to the corresponding box in the structure chart. Later, as you write the code for a module, you can put a 'C' next to the corresponding box.

Once you have finished designing and coding this program yourself, you are encouraged to read carefully the code provided here. If you discover differences, do not automatically assume that your design or code needs to be changed. There is more than way to write a program. Analyze the differences you find and ask the following questions:

    Do your design and code accomplish the same goal only in a different way?
    Is one way more efficient or are they equally good?

    Do your design and code represent a different way of interfacing with the user?
    Which way is better or are they equally good?

    Do your design and code handle the same situations or possible problems? Does they handle more situations or less? For example, which design does a better job of handling incorrect user entries?


VIII. Final Thought
Note that we have not made any changes to the contract class. This chapter focused strictly on the code that used this class. In the end, of course, we still need to put it all together just as we did in chapter five. You should follow the same instructions to get this program to work that you used to get the program from chapter five running. Remember that the only way to really make sure that code works is to run it and carefully examine its outputs given different inputs.

Next Page